package com.jcl.cloud.services.unicrawler.service;


import com.alibaba.fastjson.JSON;
import com.jcl.cloud.services.gateway.common.task.CrawlTaskReq;
import com.jcl.cloud.services.gateway.common.task.TaskContent;
import com.jcl.cloud.services.unicrawler.common.dto.CrawlTaskMgrReq;
import com.jcl.cloud.services.unicrawler.common.util.JedisUtil;
import com.jcl.cloud.services.unicrawler.util.AccountUtils;
import com.netflix.astyanax.util.TimeUUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisCommands;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.List;

@Service
public class TimeCreateTaskService {



    @Autowired
    private CrawlerManageService crawlerManageService;

    @Autowired
    private JedisPool jedisPool;

    private JedisCluster jedisCluster;

    @Scheduled(cron = "0 0 8 * * ?")
    public void createTask(){

        JedisCommands commands = null;
        try {
            commands = JedisUtil.getJedisCommands(jedisCluster, jedisPool);
            Jedis jedis = (Jedis) commands;

            jedis.flushAll();

            Thread.sleep(5*60*1000);
            jedis.flushAll();

            List<CrawlTaskMgrReq> list = this.newCrawlTaskMgrReq();
            for(CrawlTaskMgrReq crawlTaskMgrReq : list){
                this.crawlerManageService.createCrawlTask(crawlTaskMgrReq);
                Thread.sleep(60*1000);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JedisUtil.releaseJedisConnection(commands);
        }


    }



    private List<CrawlTaskMgrReq> newCrawlTaskMgrReq(){
        String str1 = "{\"account\":\"泛微网络SH\",\"cron\":\"1535167210953-9223372036854775807\",\"level\":\"1\",\"passWord\":\"603039@qq.\",\"siteCode\":\"qcwy\",\"taskType\":\"LOGIN\",\"tenantId\":\"4564356\",\"traceId\":\"123\",\"userName\":\"fanwei\"}\n";
        String str2 = "{\"account\":\"\",\"cron\":\"1535167210953-9223372036854775807\",\"level\":\"1\",\"passWord\":\"fanwei2018@\",\"siteCode\":\"zlzp\",\"taskType\":\"LOGIN\",\"tenantId\":\"456\",\"traceId\":\"123\",\"userName\":\"weaveroa\"}";
        String [] strArr = {str1,str2};

        List<CrawlTaskMgrReq> list = new ArrayList<>();

        for(String str : strArr){
            CrawlTaskReq crawlTaskReq = JSON.toJavaObject(JSON.parseObject(str),CrawlTaskReq.class);
            String traceId = crawlTaskReq.getTraceId();
            String taskId = TimeUUIDUtils.getUniqueTimeUUIDinMicros().toString();
            String tenantId = crawlTaskReq.getTenantId();
            String account = crawlTaskReq.getAccount();
            String userName = crawlTaskReq.getUserName();
            String passWord = crawlTaskReq.getPassWord();
            String siteCode = crawlTaskReq.getSiteCode();
            String taskType = crawlTaskReq.getTaskType();
            String cron = crawlTaskReq.getCron();
            String level = crawlTaskReq.getLevel();

            //后期可以对请求进行监控处理 ip  账号
            TaskContent taskContent = new TaskContent();
            taskContent.setTraceId(traceId);
            taskContent.setTaskId(taskId);
            taskContent.setSiteCode(siteCode);
            taskContent.setTaskType(taskType);
            taskContent.setTenantId(tenantId);
            taskContent.setAccount(account);
            taskContent.setUserName(userName);
            taskContent.setPassWord(passWord);
            taskContent.setAccountKey(AccountUtils.getAccountKey(account, userName, passWord));
            taskContent.setCron(cron);
            taskContent.setLevel(level);


            CrawlTaskMgrReq crawlTaskMgrReq = new CrawlTaskMgrReq();
            crawlTaskMgrReq.setTaskId(taskContent.getTaskId());
            crawlTaskMgrReq.setTenantId(taskContent.getTenantId());

            crawlTaskMgrReq.setAccountKey(taskContent.getAccountKey());
            crawlTaskMgrReq.setAccount(taskContent.getAccount());
            crawlTaskMgrReq.setUserName(taskContent.getUserName());
            crawlTaskMgrReq.setPassWord(taskContent.getPassWord());
            crawlTaskMgrReq.setSiteCode(taskContent.getSiteCode());
            crawlTaskMgrReq.setTaskType(taskContent.getTaskType());
            crawlTaskMgrReq.setCron(taskContent.getCron());
            crawlTaskMgrReq.setLevel(taskContent.getLevel());


            list.add(crawlTaskMgrReq);
        }


        return list;
    }




}
